package cn.tedu.project.oa.sys.department.service.impl;

import cn.tedu.project.oa.base.exception.ServiceException;
import cn.tedu.project.oa.base.response.StatusCode;
import cn.tedu.project.oa.sys.department.mapper.DepartmentMapper;
import cn.tedu.project.oa.sys.department.pojo.dto.AddDepartPara;
import cn.tedu.project.oa.sys.department.pojo.dto.EditDepartPara;
import cn.tedu.project.oa.sys.department.pojo.dto.QueryDepartPara;
import cn.tedu.project.oa.sys.department.pojo.entity.Department;
import cn.tedu.project.oa.sys.department.pojo.vo.DepartmentVO;
import cn.tedu.project.oa.sys.department.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DepartmentMapper deptMapper;

    /**
     * 新增部门 业务实现
     * @param addDepartPara 新增部门参数
     */
    @Override
    public void addDepartment(AddDepartPara addDepartPara) {
        log.debug("新增部门业务 addDepartPara={}", addDepartPara);
        Department department = new Department();
        BeanUtils.copyProperties(addDepartPara, department);
        deptMapper.insertDepartment(department);
        // TODO 部门字段是否唯一？
    }

    /**
     * 删除部门 业务实现
     * @param deptId 部门id
     */
    @Override
    public void deleteDepartment(Long deptId) {
        log.debug("删除部门业务 deptId={}", deptId);
        if (deptId == null || deptId <= 0) {
            throw new ServiceException(StatusCode.VALIDATE_ERROR);
        }
        deptMapper.deleteDepartmentById(deptId);
    }

    /**
     * 编辑部门 业务实现
     * @param editDepartPara 编辑部门参数
     */
    @Override
    public void EditDepartment(EditDepartPara editDepartPara) {
        log.debug("编辑部门业务 departPara={}", editDepartPara);
        Department department = new Department();
        BeanUtils.copyProperties(editDepartPara, department);
        deptMapper.updateDepartment(department);
    }

    @Override
    public List<DepartmentVO> getDepartmentList(QueryDepartPara queryDepartPara) {
        log.debug("获取部门列表业务 queryDepartPara={}", queryDepartPara);
        List<DepartmentVO> departmentList = deptMapper.selectDepartmentList(queryDepartPara);
        if (departmentList == null) {
            throw new ServiceException(StatusCode.VALIDATE_ERROR);
        }
        //获取上级部门名称
        for (DepartmentVO departmentVO : departmentList) {
            if (departmentVO.getParentId() == null) {
                //无上级部门
                departmentVO.setParentName(departmentVO.getDepartmentName());
            }else {
                String departmentName = deptMapper.selectDepartmentById(departmentVO.getParentId()).getDepartmentName();
                departmentVO.setParentName(departmentName);
            }
        }
        log.debug("获取部门列表业务 departmentList={}", departmentList);
        return departmentList;
    }

    @Override
    public List<DepartmentVO> getDepartmentChildren(Long deptId) {
        log.debug("获取部门子节点业务 deptId={}", deptId);
        QueryDepartPara queryDepartPara = new QueryDepartPara();
        queryDepartPara.setParentId(deptId);
        List<DepartmentVO> departmentList = deptMapper.selectDepartmentList(queryDepartPara);
        return departmentList;
    }

    @Override
    public int count() {
        log.debug("查询部门总数业务");

        int num = deptMapper.count();

        return num;
    }
}
